package com.zhouke.certificate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhouke.certificate.common.Constants;
import com.zhouke.certificate.controller.dto.UserDTO;
import com.zhouke.certificate.entity.Menu;
import com.zhouke.certificate.entity.User;
import com.zhouke.certificate.exception.ServiceException;
import com.zhouke.certificate.mapper.RoleMapper;
import com.zhouke.certificate.mapper.RoleMenuMapper;
import com.zhouke.certificate.mapper.UserMapper;
import com.zhouke.certificate.service.IMenuService;
import com.zhouke.certificate.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhouke.certificate.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 周科
 * @since 2022-03-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private IMenuService menuService;
    @Resource
    private UserMapper userMapper;
    @Override
    public UserDTO login(UserDTO userDTO) {
        User one=getUserInfo(userDTO);
        if(one!=null){
            BeanUtil.copyProperties(one,userDTO,true);
            // 设置token
            String token = TokenUtils.genToken(one.getId().toString(), one.getPassword());
            userDTO.setToken(token);

            String role = one.getRole(); // ROLE_ADMIN
            Integer roleId = roleMapper.selectByFlag(role);
            //设置用户菜单列表
            userDTO.setMenus(getRoleMenus(roleId));
            return userDTO;
        }else {
            throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
        }
    }

    @Override
    public User register(UserDTO userDTO) {
        User one=getUserInfo(userDTO);

        if(one==null){
            one=new User();
            BeanUtil.copyProperties(userDTO,one,true);
            one.setRole("ROLE_USER");//注册的都是普通用户
            save(one);
        }else {
            throw new ServiceException(Constants.CODE_600,"用户已存在");
        }
        return one;
    }

    @Override
    public List<User> getUserByName(String name) {
        return userMapper.selectByUsername(name);
    }

    @Override
    public Page<User> findPage(Page<User> page, String realName, String college, String identity) {
        return userMapper.findPage(page,realName,college,identity);
    }

    private User getUserInfo(UserDTO userDTO){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one;
        try{
            one = getOne(queryWrapper);

        }catch (Exception e){
            throw new ServiceException(Constants.CODE_600,"系统错误");
        }
        return one;
    }
    //根据角色id 获取当前角色菜单列表
    private List<Menu> getRoleMenus(Integer roleId){
        //当前角色所有菜单id集合
        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);
        //查出系统所有菜单
        List<Menu> menus = menuService.findMenus("", "");


        //筛选当前用户菜单
        //筛选子菜单，子菜单不在用户菜单中则肯定不在 而父菜单不在用户菜单中可能是因为父菜单中有部分子菜单在用户菜单中
        for(Menu menu:menus){ //所有菜单  引用形式
            //所有菜单的子菜单
            List<Menu> children = menu.getChildren();//菜单的子菜单  引用形式，对menu中的children 进行修改会影响menus
            //对于该子菜单的每一个菜单，如果不在角色的菜单中，则移除该菜单
            children.removeIf(child->!menuIds.contains(child.getId()));
        }

        menus.removeIf(menu->(!menuIds.contains(menu.getId()))&&menu.getChildren().size()==0);

        return menus;
    }
}
